package com.wangsd.web.face.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangsd.web.face.entity.Agent;
import com.wangsd.web.face.entity.ServiceBusiness;
import com.wangsd.web.face.mapper.AgentMapper;
import com.wangsd.web.face.service.AgentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangsd.web.face.service.ServiceBusinessService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-09-09
 */
@Service
public class AgentServiceImpl extends ServiceImpl<AgentMapper, Agent> implements AgentService {

    @Resource
    AgentMapper agentMapper;
    @Autowired
    ServiceBusinessService serviceBusinessService;

    @Override
    public IPage<Agent> selectList(Integer pageNum, Integer pageSize, Agent params, String code) {
        Page<Agent> page = new Page<>(pageNum, pageSize);
        IPage<Agent> resultIPage = agentMapper.selectListXml(page, params, code);
        return resultIPage;
    }

    @Override
    @Transactional
    public void addOrUpdate(Agent params) {
        if (params.getId() != null) {
            agentMapper.updateById(params);
        } else {
            agentMapper.insert(params);

            //保留检索CODE
            ServiceBusiness serviceBusiness = serviceBusinessService.getById(params.getBusinessId());
            params.setCode(serviceBusiness.getCode() + "-" + params.getId());
            agentMapper.updateById(params);
        }
    }

    @Override
    public List<Map> selectAllByUserCode(String code) {
        List<Map> resultList = new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.likeRight("code", code);
        }
        queryWrapper.eq("deleted", "0");
        List<Agent> list = agentMapper.selectList(queryWrapper);
        for (Agent agent : list) {
            Map map = new HashMap();
            map.put("id", agent.getId());
            map.put("code", agent.getCode());
            map.put("name", agent.getName());
            resultList.add(map);
        }
        return resultList;
    }

    @Override
    public List<Agent> selectAllByCode(String code) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.likeRight("code", code);
        }
        queryWrapper.eq("deleted", "0");
        List<Agent> list = agentMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<Agent> selectByCode(String code) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.eq("code", code);
        }
        queryWrapper.eq("deleted", "0");
        List<Agent> list = agentMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public Integer selectCountByBusinessId(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("business_id", id);
        queryWrapper.eq("deleted", "0");
        Integer count = agentMapper.selectCount(queryWrapper);
        return count;
    }
}
